package ae.pcfc.etks.online.itworx.ehs.web.dao.impl;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import ae.pcfc.etks.online.itworx.ehs.web.dao.EHSDAO;
import ae.pcfc.etks.online.itworx.ehs.web.entity.DocumentType;
import ae.pcfc.etks.online.itworx.ehs.web.entity.OFCDocument;
import ae.pcfc.etks.online.itworx.ehs.web.enums.RequestStatus;

@Repository
public class EHSDAOImpl extends BaseDAOImpl implements EHSDAO {

	public List<DocumentType> getAllDocumentType() {
		Session sessionFactory = hibernateTemplate.getSessionFactory().getCurrentSession();
		return sessionFactory.createQuery("From DocumentType where status='A' order by id asc").list();
	}

	public void createNewDocumentType(DocumentType documentType) {
		saveObject(documentType);
		
	}

	public void deleteDocumentType(DocumentType documentType) {
		//removeObject(documentType);
		
		//Session sessionFactory = hibernateTemplate.getSessionFactory().getCurrentSession();
		//sessionFactory.createQuery("Update DocumentType set status ='removed' where ID=?").setLong(0, documentType.getId());//getObject(DocumentType.class, id);
		updateObject(documentType);
		
		
	}

	public void updateDocumentType(DocumentType documentType) {
		updateObject(documentType);
		
	}

	public DocumentType retrieveDocumentTypeByName(DocumentType documentType) {
		Session sessionFactory = hibernateTemplate.getSessionFactory().getCurrentSession();
		if(documentType.getId()!=0){
			return (DocumentType)sessionFactory.createQuery("From DocumentType Where status='A' and (lower(arabicName)=? or lower(englishName)=?) and id!=?").setString(0, documentType.getArabicName().toLowerCase()).setString(1, documentType.getEnglishName().toLowerCase()).setLong(2, documentType.getId()).uniqueResult();
		}else{
			return (DocumentType)sessionFactory.createQuery("From DocumentType Where status='A' and (lower(arabicName)=? or lower(englishName)=?)").setString(0, documentType.getArabicName().toLowerCase()).setString(1, documentType.getEnglishName().toLowerCase()).uniqueResult();
		}
		
	}

	public DocumentType getDocumentTypeById(Long id) {
		
		Criteria dbCriteria = getHibernateTemplate().getSessionFactory()
		.getCurrentSession().createCriteria(DocumentType.class);
		
		dbCriteria.add(Restrictions.eq("status", "A"));
		dbCriteria.add(Restrictions.eq("id", id));
		//dbCriteria.add(Restrictions.)
		
		DocumentType dt = (DocumentType) dbCriteria.list().get(0);
		
		//Session sessionFactory = hibernateTemplate.getSessionFactory().getCurrentSession();
		//DocumentType dt = (DocumentType) sessionFactory.createQuery("From DocumentType Where status ='exist' and ID=?").setLong(0, id);//getObject(DocumentType.class, id);
		
		//hibernateTemplate.evict(dt);
		return dt;
	}

	@Override
	public OFCDocument findOFCDocumentById(long id) {
		// TODO Auto-generated method stub
		return (OFCDocument) getObject(OFCDocument.class, id);
	}
	
	public List<DocumentType> getAllDocumentTypeByOFCStatus(String documentName, RequestStatus requestStatus){
		Criteria dbCriteria = getHibernateTemplate().getSessionFactory()
		.getCurrentSession().createCriteria(DocumentType.class);
		if(null != requestStatus && (requestStatus.equals(RequestStatus.Additional_Payment_Required) || requestStatus.equals(RequestStatus.Approved))){
			dbCriteria.add(Restrictions.ilike("englishName", documentName));
		}else{
			dbCriteria.add(Restrictions.ne("englishName", documentName));
		}
		return dbCriteria.list();
		
	}

}
